from typing import Optional

from fastapi import APIRouter, Query, Body, Request
from fastapi_utils.cbv import cbv
from fastapi_utils.inferring_router import InferringRouter

from server.app.cloud_ide.schema.users.request.user_create_request import UserCreateRequest
from server.app.cloud_ide.schema.users.request.user_role_update_request import UserRoleUpdateRequest
from server.app.cloud_ide.schema.users.request.user_update_password_request import UserUpdatePassWordRequest
from server.app.cloud_ide.schema.users.request.user_update_request import UserUpdateRequest
from server.app.cloud_ide.schema.users.response.user_detail_entity import UserDetailEntity
from server.app.cloud_ide.service.user_service import UserService
from server.framework.core.http_schemas import CommonResponseSchema, TableResponseSchema

user_api = InferringRouter()


@cbv(user_api)
class UserApi:
    user_service = UserService()

    @user_api.get("/list", response_model=TableResponseSchema, name='获取用户列表')
    async def list(self, current: Optional[int] = Query(1, description='当前页数'),
                   page_size: Optional[int] = Query(10, description='每页记录的数量',
                                                    alias='pageSize')) -> TableResponseSchema:
        users = self.user_service.list_users()
        return TableResponseSchema(data=users[current - 1:current * page_size], total=len(users),
                                   page=page_size, success=True)

    @user_api.post("/create", response_model=CommonResponseSchema, name='创建用户')
    async def create(self, data: UserCreateRequest = Body(None, description='用户创建实体')) -> CommonResponseSchema:
        result = self.user_service.create_user(data)
        if result is True:
            return CommonResponseSchema(data={}, message=f'创建用户[{data.username}]成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'创建用户[{data.username}]失败', success=False)

    @user_api.get("/get_entity", name='获取用户实体')
    async def get_entity(self, id: str = Query(None, description='用户id')) -> UserDetailEntity:
        return self.user_service.get_user_detail(id)

    @user_api.post("/edit", response_model=CommonResponseSchema, name='更新用户')
    async def edit(self, data: UserUpdateRequest = Body(None, description='用户更新实体')) -> CommonResponseSchema:
        result = self.user_service.update_user_info(data)
        if result is True:
            return CommonResponseSchema(data={}, message=f'更新用户[{data.first_name}]成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'更新用户[{data.first_name}]失败', success=True)

    @user_api.get("/delete", response_model=CommonResponseSchema, name='删除用户')
    async def delete(
            self, ids: str = Query(None, description='用户id，多条记录以逗号分割')) -> CommonResponseSchema:
        delete_ids = ids.split(',')
        result = self.user_service.delete_users(delete_ids)
        if result is True:
            return CommonResponseSchema(data={}, message=f'删除成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'删除失败', success=False)

    @user_api.post("/change_password", response_model=CommonResponseSchema, name='修改用户密码')
    async def change_password(
            self, request: Request,
            data: UserUpdatePassWordRequest = Body(None, description='修改用户密码实体')) -> CommonResponseSchema:
        if request.state.is_admin:
            data.operator_is_admin = True
        else:
            data.operator_is_admin = False
        result = self.user_service.change_password(data)
        if result:
            return CommonResponseSchema(data={}, message=f'修改用户密码成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'修改用户密码失败', success=False)

    @user_api.post("/edit_user_group", response_model=CommonResponseSchema, name='修改用户所属组')
    async def edit_user_group(self,
                              data: UserRoleUpdateRequest = Body(None,
                                                                 description='修改用户所属组实体')) -> CommonResponseSchema:
        rs = self.user_service.set_user_roles(data)
        if rs:
            return CommonResponseSchema(data={}, message=f'修改成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'修改失败', success=False)
